﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Model;

namespace University.Workshop.DataAccess
{
    public class DataAccessCollectControl
    {
        public static ICollection<Model.Payment> SearchPaymentByDatesAndClientId(Model.Payment chargeToFind)
        {
            WFPEntities.Instance.DiscardChanges();

            var q = from c in WFPEntities.Instance.Payment

                    where (c.PaymentDate >= chargeToFind.InitialDate
                    && c.PaymentDate <= chargeToFind.FinalDate
                    && c.KardexId.Equals(chargeToFind.KardexId))
                    select c;
            return q.ToList();
        }

        public static ICollection<Model.Payment> SearchPaymentByDatesMinusClientId(Model.Payment chargeToFind)
        {
            WFPEntities.Instance.DiscardChanges();

            var q = from c in WFPEntities.Instance.Payment
                    where (c.PaymentDate >= chargeToFind.InitialDate
                    && c.PaymentDate <= chargeToFind.FinalDate)
                    select c;
            return q.ToList();
        }

        public static ICollection<Model.CollectControl> SearchCollectsByPaymentId(Model.Payment paymentToFind)
        {
            WFPEntities.Instance.DiscardChanges();

            List<Model.CollectControl> coleccion = new List<Model.CollectControl>();
            String listaCobros = paymentToFind.Charges;
            String[] cobrosStr = listaCobros.Split(';');
            int[] cobros;

            if (cobrosStr.Length > 1)
            {
                cobros = new int[cobrosStr.Length - 1];
                for (int i = 0; i < cobrosStr.Length - 1; i++)
                    cobros[i] = int.Parse(cobrosStr[i]);
            }
            else
            {
                cobros = new int[cobrosStr.Length];
                cobros[0] = int.Parse(cobrosStr[0]);
            }

            CollectControl cdb = new CollectControl();
            foreach (int collect in cobros)
            {
                cdb = (from c in WFPEntities.Instance.CollectsControls
                       where (c.CollectId.Equals(collect))
                       select c).First();

                coleccion.Add(cdb);
            }

            return coleccion;
        }

        public static ICollection<Model.WaterCollectControl> SearchExpenseByParameterAll(Model.WaterCollectControl chargeToFind)
        {
            WFPEntities.Instance.DiscardChanges();
            
            //if (chargeToFind.CollectIsPaid == 3)
            if (chargeToFind.IsPaid)
            {
                var q = from c in WFPEntities.Instance.WaterCollectControl
                        where (//c.KardexId.Equals(chargeToFind.KardexId) &&
                              c.ReadingDate.Equals(chargeToFind.ReadingDate))
                        select c;
                return q.ToList();
            }
            else
            {
                var q = from c in WFPEntities.Instance.WaterCollectControl
                        where (//c.KardexId.Equals(chargeToFind.KardexId) &&
                              c.IsPaid.Equals(chargeToFind.IsPaid) &&
                              c.ReadingDate.Equals(chargeToFind.ReadingDate))
                        select c;
                return q.ToList();
            }
        }


        public static ICollection<Model.WaterCollectControl> SearchExpenseMinusDatePurchase(Model.WaterCollectControl chargeToFind)
        {
            WFPEntities.Instance.DiscardChanges();
            
            //if (chargeToFind.CollectIsPaid == 3)
            if (chargeToFind.IsPaid)
            {
                var q = from c in WFPEntities.Instance.WaterCollectControl
                        //where (c.KardexId.Equals(chargeToFind.KardexId))
                        select c;
                return q.ToList();
            }
            else
            {
                var q = from c in WFPEntities.Instance.WaterCollectControl
                        where (c.IsPaid.Equals(chargeToFind.IsPaid))
                        //&& c.KardexId.Equals(chargeToFind.KardexId))
                        select c;
                return q.ToList();
            }


        }



       /*
        public static ICollection<Model.CollectControl> SearchChargeMinusDatePurchase(Model.CollectControl chargeToFind)
        {




            /*WFPEntities.Instance.DiscardChanges();

            List<Model.CollectControl> lista = new List<Model.CollectControl>();
            FineCollectControl f = new FineCollectControl();
            WaterCollectControl w = new WaterCollectControl();


            
            var q = from c in WFPEntities.Instance.FineCollectControl
                    where (c.FineCollectIsPaid.Equals(false))
                    //&& c.KardexId.Equals(chargeToFind.KardexId))
                    select c;

            lista.Union(q.ToList()).ToList();

            var r = from c in WFPEntities.Instance.WaterCollectControl
                    where (c.WaterCollectControlIsPaid.Equals(false))
                    select c;

            lista.Union(r.ToList()).ToList();

           
            List<Model.CollectControl> collects = (from c in WFPEntities.Instance.CollectsControls
                select c).ToList();

            lista.Union(collects).ToList();*/



            //return lista;
   /*     }
    */
    }
}
